package com.nageoffer.shortlink.project.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nageoffer.shortlink.project.dao.entity.LinkOsStatsDO;
import com.nageoffer.shortlink.project.dto.req.ShortLinkStatsReqDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.HashMap;
import java.util.List;

/**
 * ClassName: LinkOsStatsMapper
 * Package:
 * Description:  操作系统访问持久层
 *
 * @Author:xiaohei
 * @Create 2024/6/12 9:29
 * Version 1.0
 */
public interface LinkOsStatsMapper extends BaseMapper<LinkOsStatsDO> {

    @Insert("INSERT INTO t_link_os_stats (full_short_url, gid, date, cnt,os, create_time, update_time, del_flag) " +
            "VALUES (" +
            "    #{LinkOsStats.full_short_url}," +
            "    #{LinkOsStats.gid}," +
            "    #{LinkOsStats.date}," +
            "    #{LinkOsStats.cnt}," +
            "    #{LinkOsStats.os}," +
            "    NOW()," +
            "    NOW()," +
            "    0" +
            ")" +
            "ON DUPLICATE KEY UPDATE" +
            "    cnt = cnt + #{LinkOsStats.cnt};")
    void shortLinkOsStats(@Param("LinkOsStats") LinkOsStatsDO LinkOsStatsDO);

    /**
     * 根据短链接获取指定日期内操作系统监控数据
     */
    @Select("SELECT " +
            "    os, " +
            "    SUM(cnt) AS count " +
            "FROM " +
            "    t_link_os_stats " +
            "WHERE " +
            "    full_short_url = #{requestParam.fullShortUrl} " +
            "    AND gid = #{requestParam.gid} " +
            "    AND date BETWEEN #{requestParam.startDate} and #{requestParam.endDate} " +
            "GROUP BY " +
            "    full_short_url, gid, date, os;")
    List<HashMap<String, Object>> listOsStatsByShortLink(@Param("requestParam") ShortLinkStatsReqDTO requestParam);
}
